package cn.interestingshop.dao;



import cn.interestingshop.entity.Notice;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NoticeDaoImpl extends BaseDaoImpl implements NoticeDao{
    public NoticeDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<Notice> createEntityByResultSet(ResultSet rs) throws Exception {


        List<Notice> list= new ArrayList<>();

        while(rs.next()){

            Notice notice=new Notice();

            notice.setId(rs.getInt("id"));
            notice.setContent(rs.getString("content"));
            notice.setCreateTime(rs.getString("create_time"));
            notice.setTitle(rs.getString("title"));

            list.add(notice);
        }


        return list;
    }

    public Notice createEntityByResultSet2(ResultSet rs) throws Exception {


        Notice notice=new Notice();

        if(rs.next()){



            notice.setId(rs.getInt("id"));
            notice.setContent(rs.getString("content"));
            notice.setCreateTime(rs.getString("create_time"));
            notice.setTitle(rs.getString("title"));


        }


        return notice;
    }

    @Override
    public List<Notice> selectAll(Integer currentPageNo, Integer pageSize) {

        ResultSet rs=null;
        List<Notice> list=new ArrayList<>();

        try{
            String sql="select id,title,content,create_time from t_notice";
            sql += " limit " + (currentPageNo - 1) * pageSize + "," + pageSize;
            rs=executeQuery(sql,null);

            list=createEntityByResultSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }

        return list;
    }

    @Override
    public List<Notice> selectAll() {
        ResultSet rs=null;
        List<Notice> list=new ArrayList<>();

        try{
            String sql="select id,title,content,create_time from t_notice";
            rs=executeQuery(sql,null);

            list=createEntityByResultSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }

        return list;
    }

    @Override
    public Notice selectOne(int id) {
        ResultSet rs=null;
        Notice notice=new Notice();

        try{
            String sql="select id,title,content,create_time from t_notice where id="+id;
            rs=executeQuery(sql,null);

            notice=createEntityByResultSet2(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }

        return notice;
    }

    @Override
    public int selectAllCount() {
        ResultSet rs=null;
        int i=0;

        try{
            String sql="select  count(1) from t_notice";
            rs=executeQuery(sql,null);

            if(rs.next()){
                i=rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }

        return i;
    }
}
